package com.charles.springcloud.alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.charles.common.CommonResult;
import com.charles.common.entity.Payment;
import com.charles.springcloud.alibaba.handler.CustomerBlockHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: 风中灵劲草
 * @Date: 2021/08/26/15:59
 * @Description:
 */
@RestController
@Slf4j
public class RateLimitController {


    @GetMapping("/byResource")
    @SentinelResource(value = "byResource" , blockHandler = "handleException")
    public CommonResult byResource(){
        return new CommonResult(200 , "按资源名称限流测试", new Payment(2021L , "serial001"));
    }

    public CommonResult handleException(BlockException exception){
        return new CommonResult(444 , exception.getClass().getCanonicalName()+"\t"+"服务不可用");
    }
    @GetMapping("/rateLimit/customBlockHandler")
    @SentinelResource(value = "customBlockHandler" , blockHandlerClass = CustomerBlockHandler.class , blockHandler = "handlerException2")
    public CommonResult customBlockHandler(){
        return new CommonResult(200 , "按客户自定义", new Payment(2021L , "serial003"));
    }

}
